﻿using Asp.Versioning;
using FlyingEye.Services.SignalRHubs;
using FlyingEye.SignalRHubs.Requests;
using FlyingEye.SignalRHubs.TaskResults;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [Authorize]
    [ControllerName("DeviceHub")]
    [ApiVersion("1.0")]
    [Route("api/device-app-service/v{version:apiVersion}/device-clients")]
    public class DeviceHubController : DefaultController
    {
        private readonly DeviceHubService _deviceHubService;

        public DeviceHubController(
            DeviceHubService deviceHubService)
        {
            _deviceHubService = deviceHubService;
        }

        [HttpPost("{id}/config")]
        public async Task<ActionResult<DefaultResponse>> ExecuteAsync(string id)
        {
            return await this.ActionInvokeAsync(async () =>
            {
                return await _deviceHubService.ExecuteAsync(id);
            });
        }

        [HttpPost("{id}/read-numeric-value")]
        public async Task<ActionResult<ReadValueResponse>> ExecuteAsync(string id, [FromBody] ReadNumericValueRequest request)
        {
            return await this.ActionInvokeAsync(async () =>
            {
                return await _deviceHubService.ExecuteAsync(id, request);
            });
        }

        [HttpPost("{id}/read-string-value")]
        public async Task<ActionResult<ReadValueResponse>> ExecuteAsync(string id, [FromBody] ReadStringValueRequest request)
        {
            return await this.ActionInvokeAsync(async () =>
            {
                return await _deviceHubService.ExecuteAsync(id, request);
            });
        }

        [HttpPost("{id}/write-value")]
        public async Task<ActionResult<DefaultResponse>> ExecuteAsync(string id, [FromBody] WriteValueRequest request)
        {
            return await this.ActionInvokeAsync(async () =>
            {
                return await _deviceHubService.ExecuteAsync(id, request);
            });
        }
    }
}
